Basics
Using PHP in your SUCS webspace is as easy as creating a file with a .php extention.
e.g. Create a file called helloworld.php
<?php
echo "Hello World<br>\n";
?>
There is comprehensive documentation for php online at http://www.php.net
Programming
For those that have never programmed for the web before, web (or CGI) programming is quite different from writing traditional software. An easy way to think of what happens is that every time a user loads a web page, your program gets run, it gets told any values the user gave, it prints out a response page and then exits.
Your program (your php page) exits after every request, nothing is stored, there is no kind of state or persistance, you have to do all of those things yourself.
Typical approaches that are used to get around these are :-
Cookies
When you output a page you can include with it some small pieces of text called cookies. The users web browser will store these, and hand a copy of them back to you with each subsequent page request. These are often used to remember some kind of state to identify which user it is returning, such as by storing a unique session identifier.
Databases
Cookies can only hold small amounts of data, and are not very permanent or trustworthy, if a user closes the browser, or switches to another machine, they are lost. For more persistant storage we typically use a database. Ask a member of SUCS admin to create one for you.
Using a Database
Here at SUCS we prefer to use the PostgreSQL database system, it is a very common and powerful SQL database system, once a database has been created you can directly access the database from the commandline using the psql command. You can also access it from your PHP code, PHP does have functions for directly interface with postgres, but we would recommend that you use an abstraction layer such as AdoDB, which makes accessing it much friendlier and safer.
AdoDB is already installed on the SUCS servers, to use it simply include the following lines in your code :
require("/usr/share/php/adodb/adodb.inc.php");
$DB = NewADOConnection('postgres8');
$DB->Connect('dbname=mydbname user=myusername');
$DB->SetFetchMode(ADODB_FETCH_ASSOC);
You can then make database queries like so. (see the Adodb Manual for more info)
$rows = $DB->GetAll("SELECT * FROM mydata ORDER BY id");
The result is an array $rows where each element of the array contains one row of the database as an associative array.
An example for printing all the results might be something like this:
echo "<ul>\n"; foreach ($rows as $row) { echo "<li>${row['id']} = ${row['name']}\n"; } echo "<ul>\n";
Using Templates
Traditional PHP can be thought of as a HTML page with fragments of code within it, where the fragments of code control the printing of certain other parts of the HTML. This is easy when you start, but as your projects grow in complexity it can become harder deal with the mixture of code and html. One good way of dealing with this is to seperate the html generation away from the PHP code by using a templating system such as Smarty.
You can use the smarty system by first creating the directories 'templates' and 'templates_c' in your directory, then add the following code to your PHP document, making sure that there is nothing before the <?php tag.
<?php
require("/usr/share/php/smarty/libs/Smarty.class.php");
$smarty = new Smarty;
Now, to reuse the above database example, we would make out database query, and then tell smarty about the data we want it to print.
$rows = $DB->GetAll("SELECT * FROM mydata ORDER BY id");
$smarty->assign("mydata", $rows);
Next we define the template, which tells smarty how to display the data.
We create the file templates/list.html and enter this:
<html>
<body>
My List :-
<ul>
{foreach $mydata as $row}
<li> {$row.id} = {$row.name}
{/foreach}
</ul>
</body>
</html>
Now that we have both the data, and the instructions on how to display it, back in our php code we can ask it to display the result.
$smarty->display("list.html");
?>
We can exit then as we have finished and dont need to output anything more.
If you write your templates carefully, you will quickly find that you can resuse the same templates again and again for different parts of your website, especially if you start to write reusable templates that only render certain pieces of a page, and you chain them together. eg by using {include ...} to load common headers and footers for each page.